<template>
  <div>
    <!--
        action为必须项,作为上传地址，我们使用云服务，所以用#敷衍一下
        fileList 管理图片的数组
        list-type 设置照片墙样式
        on-preview:点击文件列表中已上传的文件时的钩子
        on-remove:文件列表移除文件时的钩子
        on-change:文件状态改变时的钩子，添加文件、上传成功和上传失败时都会被调用
        http-request:覆盖action默认的上传行为，可以自定义上传的实现
        before-upload:上传文件之前的钩子，参数为上传的文件，若返回 false 或者返回 Promise 且被 reject，则停止上传
        limit:最大允许上传个数
     -->
    <el-upload
      list-type="picture-card"
      action="#"
      :file-list="fileList"
      :class="{disable:fileList.length>0}"
      :on-preview="onPreview"
      :on-remove="onRemove"
      :on-change="onChange"
      :http-request="upload"
      :before-upload="beforeUpload"
      :limit="1"
    >
      <i class="el-icon-plus" />
    </el-upload>
    <!-- 进度条 -->
    <el-progress v-show="isShowPercent" :text-inside="true" :stroke-width="26" :percentage="percent" style="width:150px" />
    <!-- 预览弹窗 -->
    <el-dialog title="预览" :visible="isShowDialog" @close="isShowDialog=false">
      <el-row type="flex" justify="center">
        <img :src="PreviewImg" alt="">
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import COS from 'cos-js-sdk-v5'
const cos = new COS({
  SecretId: 'AKIDDF2YonVx94j7E6i4AzYrte8MJzPYQ0JN',
  SecretKey: 'oMddMoyrUqvjHhwwhFZeVCeqmTu50gHh'
})
export default {
  data() {
    return {
      fileList: [],
      isShowDialog: false,
      PreviewImg: '',
      percent: 0,
      isShowPercent: false
    }
  },
  methods: {
    //   预览图片
    onPreview(file) {
      console.log(file)
      this.PreviewImg = file.url
      this.isShowDialog = true
    },
    // 移除图片
    onRemove(file, fileList) {
    //   console.log(file, fileList)
      this.fileList = fileList
    },
    // 文件状态改变时的钩子，添加文件、上传成功和上传失败时都会被调用
    onChange(file, fileList) {
    //   console.log(file, fileList)
      this.fileList = fileList
    },
    // 上传前触发的函数,用于做安全判定
    beforeUpload(file) {
      console.log(file)
      const typeList = ['image/png', 'image/jpg', 'image/jpeg', 'image/gif', 'image/bmp']
      const maxSize = 2 * 1024 * 1024
      if (!typeList.includes(file.type)) {
        this.$message.warning('图片必须为jpg/jpeg/png/gif/bmp格式')
        return false
      }
      if (file.size > maxSize) {
        this.$message.warning('图片大小不能超过2M')
        return false
      }
      return true
    },
    // 覆盖默认的上传行为,自定义上传
    upload(params) {
      console.log(params)
      this.isShowPercent = true
      cos.putObject({
        Bucket: 'yq-1308566137', /* 存储桶名*/
        Region: 'ap-guangzhou', /* 存储桶所在地域，必须字段 */
        Key: params.file.name, /* 文件名 */
        StorageClass: 'STANDARD',
        Body: params.file, // 上传文件对象
        onProgress: (progressData) => {
          console.log(JSON.stringify(progressData))
          this.percent = Math.round(progressData.percent * 100)
        }
      }, (err, data) => {
        // 腾讯云上传结束的回调
        console.log(err || data)
        this.fileList[0].url = 'http://' + data.Location
        this.fileList[0].status = 'success'
        setTimeout(() => { this.isShowPercent = false }, 800)
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.disable{
    ::v-deep .el-upload--picture-card{
        display: none;
    }
}
</style>
<style lang="scss">
img{
    object-fit: cover;
}
</style>
